package com.cn.intelligence.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbMUserMapper;
import com.cn.intelligence.mapper.TbMenuMapper;
import com.cn.intelligence.pojo.TbItemCat;
import com.cn.intelligence.pojo.TbMUser;
import com.cn.intelligence.pojo.TbMUserExample;
import com.cn.intelligence.pojo.TbMenu;
import com.cn.intelligence.pojo.TbMenuExample;
import com.cn.intelligence.service.LoginService;
@Service
public class LoginServiceImpl implements LoginService { 

	@Autowired
	public TbMUserMapper tbMUserMapper;
	@Autowired
	public TbMenuMapper tbMenuMapper;
		
	@Override
	public TbMUser getUserInfo(TbMUser tbMuser) {
		// 查询用户新
		TbMUserExample tbMUserExample = new TbMUserExample();
		TbMUserExample.Criteria criteria = tbMUserExample.createCriteria();
        criteria.andUsernameEqualTo(tbMuser.getUsername());
        criteria.andPasswordEqualTo(tbMuser.getPassword());      
        
		List<TbMUser> tbMuserList = tbMUserMapper.selectByExample(tbMUserExample);
		// 如果用户信息为空，或者size》1说明用户有问题
		if(tbMuserList.size()==1){
			return tbMuserList.get(0);
		}else{
			return null;
		}
		
	}

	@Override
	public List<Map<String, Object>> getMenuList(Long parentId,String role) {
        // 查询类目标
        TbMenuExample tbMenuExample = new TbMenuExample();
        TbMenuExample.Criteria criteria=tbMenuExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        if(role!=null && "3".equals(role) ){
        	criteria.andRoleEqualTo(role);
        }
        tbMenuExample.setOrderByClause("sort_order");
        List<TbMenu>  tbMenuList= tbMenuMapper.selectByExample(tbMenuExample);
        List<Map<String,Object>> treenodelist = new ArrayList<Map<String,Object>>();
        for (TbMenu tbMenu:tbMenuList) {
        	Map<String,Object> node = new HashMap<String,Object>();
        	node.put("id", tbMenu.getId());
        	node.put("text", tbMenu.getName());
        	node.put("state",tbMenu.getIsParent()?"closed":"open");
        	node.put("status", tbMenu.getStatus());
        	node.put("sortOrde", tbMenu.getSortOrder());
        	node.put("isParent", tbMenu.getIsParent());
        	node.put("url", tbMenu.getUrl());
        	treenodelist.add(node);
      }
        return treenodelist;
    }

}
